package com.leyuangame.core.service.impl;

import com.leyuangame.core.controller.bean.ResponseHandler.LeYuanResult;
import com.leyuangame.core.model.PlayUserSafeLog;
import com.leyuangame.core.service.UserService;
import com.leyuangame.dao.repository.jpa.PlayUserRepository;
import com.leyuangame.dao.repository.jpa.PlayUserSafeLogRepository;
import com.leyuangame.dao.repository.jpa.user.PlayUser;
import com.leyuangame.utils.MD5;
import com.leyuangame.utils.UKTools;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class UserServcieImpl implements UserService {
    @Autowired
    private PlayUserRepository playUserRepository;
    @Autowired
    private PlayUserSafeLogRepository playUserSafeLogRepository;
    @Override
    public LeYuanResult getUserSafeStatus(String token) {
        PlayUser userByToken = playUserRepository.findPlayUserByToken(token);
        if(userByToken==null)
            return LeYuanResult.build(1,"用户信息无效");
        int is_init = StringUtils.isEmpty(userByToken.getSafe_passwd())?1:0;
        return LeYuanResult.ok(new HashMap<String,Object>().put("is_init",is_init));
    }

    @Override
    public LeYuanResult updateUserSafePasswd(String token, String oldPasswd, String passwd) {
        PlayUser userByToken = playUserRepository.findPlayUserByToken(token);
        if(userByToken==null)
            return LeYuanResult.build(1,"用户信息无效");
        if(StringUtils.isEmpty(userByToken.getSafe_passwd())){
            userByToken.setSafe_passwd(UKTools.md5(passwd));
            playUserRepository.saveAndFlush(userByToken);
            return LeYuanResult.build(1,"保险箱初始化成功");
        }
        if(userByToken.getSafe_passwd().equals(UKTools.md5(oldPasswd))){
            userByToken.setSafe_passwd(UKTools.md5(passwd));
            playUserRepository.saveAndFlush(userByToken);
            return LeYuanResult.ok();
        }else {
            return LeYuanResult.build(1,"密码错误");
        }
    }

    @Override
    public LeYuanResult getUserSafeLog(String token, int type) {
        PlayUser userByToken = playUserRepository.findPlayUserByToken(token);
        if(userByToken==null)
            return LeYuanResult.build(1,"用户信息无效");
        List<PlayUserSafeLog> res = null;
        if(type!=0){
            res =playUserSafeLogRepository.findPlayUserSafeLogsByUseridAndTypeOrderByAddtimeDesc(userByToken.getId(),type);
        }else {
            res = playUserSafeLogRepository.findPlayUserSafeLogsByUseridOrderByAddtimeDesc(userByToken.getId());
        }
        Map<String,Object> result= new HashMap<>();
        result.put("safe_logs",res);
        return LeYuanResult.ok(result);
    }

    @Override
    public LeYuanResult getUserInfoByToken(String token) {
        Map<String,Object> result= new HashMap<>();
        PlayUser tokenuser = playUserRepository.findPlayUserByToken(token);
        if(tokenuser==null)
            return LeYuanResult.build(1,"用户信息无效请重新登陆");
        result.put("phone",tokenuser.getMobile());
        result.put("id",tokenuser.getId());
        result.put("token",tokenuser.getToken());
        result.put("username",tokenuser.getUsername());
        result.put("goldcoins",tokenuser.getGoldcoins());
        result.put("is_safepasswd",tokenuser.getSafe_passwd()==null?1:0);
        return LeYuanResult.ok(result);
    }
}
